package cc.lj.internal.mobilepay.interfaces.facade;

import cc.lj.internal.core.exception.SysExceptionEnum;
import cc.lj.internal.core.http.HttpResult;
import cc.lj.internal.mobilepay.LjConfig;
import cc.lj.internal.mobilepay.application.utils.FileUtil;
import cc.lj.internal.mobilepay.interfaces.dto.PayResponseDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Map;
import java.util.Properties;

/**
 * 不同方式读取文件路径 demo
 * @author liangxifeng
 * @date 2022/12/20 15:17
 */
@RestController
@RequestMapping("resource")
@Slf4j
public class ReaderFilePathDemo {
    @Autowired
    protected LjConfig ljConfig;

    @Autowired
    ResourceLoader resourceLoader;

    @GetMapping("/getResource1")
    public HttpResult<Object> getResource1() throws Exception {
        //ClassPathResource resource = new ClassPathResource(ljConfig.getPrivateKey());
        //InputStream inputStream = resource.getInputStream();
        //FileInputStream inputStream1 = (FileInputStream) inputStream;
        //ReaderFilePathDemo.showPath(inputStream);
        byte[] bytes = FileUtil.read4file(ljConfig.getPrivateKey());
        return HttpResult.success(SysExceptionEnum.SUCCESS.getCode(),SysExceptionEnum.SUCCESS.getMessage(),bytes);
        //return bytes;
    }
    public static void showPath(InputStream input) {
        Properties prop = new Properties();

        if (null == input) {
            throw new RuntimeException("can not load properties: " + input);
        }

        try {
            prop.load(input);
            input.close();
            String a = prop.getClass().getSimpleName();
            String b = prop.getClass().getName();
            for (Map.Entry<Object, Object> entry : prop.entrySet()) {
                log.info(entry.getKey().toString() );
                log.info("==================");
                log.info(entry.getValue().toString());
            }

        } catch (IOException e) {
            throw new RuntimeException("can not read properties: " + input);
        }
    }
}
